/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package graphics;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.VolatileImage;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import stealth.Main;

/**
 * TODO: Implement RenderingThread class Javadoc
 * @author Joshua Mabrey
 */
public class RenderingThread extends AbstractRenderer implements Runnable {

    private VolatileImage background;

    public RenderingThread() {
        renderingObjects = new ArrayList<RenderInstruction>();
        graphics = null;
        Thread thread = new Thread(this);
        thread.setName("Render Thread");
        thread.start();
    }
    private ArrayList<RenderInstruction> renderingObjects;
    private boolean continueRunning = true;
    private boolean startRender = false;
    private boolean startedThread = false;
    private Graphics2D graphics;

    public synchronized void addRenderInstruction(RenderInstruction renderOp) {
        renderingObjects.add(renderOp);
    }

    public boolean finishedRendering() {
        return renderingObjects.isEmpty();
    }

    public synchronized void renderAll(Graphics2D g) {
        if (g == null) {
            throw new NullPointerException("The renderer cannot use a null graphics context");
        }
        graphics = g;
        startRender = true;
    }

    public void endImmediately() {
        continueRunning = false;
    }

    @Override
    public void run() {
        while (continueRunning) {
            if (startRender) {
                graphics.clearRect(0, 0, Main.game.getWidth(), Main.game.getHeight());
                for (int i = 0; (double) i < (double) Main.game.getWidth() / background.getWidth(); i++) {
                    for (int j = 0; (double) j < (double) Main.game.getHeight() / background.getHeight(); j++) {
                        graphics.drawImage(background, i * background.getWidth(), j * background.getHeight() + 60, null);
                    }
                }
                for (RenderInstruction renderOp : renderingObjects.toArray(new RenderInstruction[0])) {
                    //System.out.println("Inside render loop for");
                    try {
                        renderOp.render(graphics);
                    } catch (NullPointerException e) {
                        System.err.println(e);
                    }
                }
                renderingObjects.clear();

            } else {
                try {
                    this.wait(10);
                } catch (Exception ex) {
                    //just continue
                }
            }
        }
        startRender = false;
    }

    @Override
    public void setBackgroundImage(Image i) {
        if (i == null) {
            return;
        }
        /**
         * TODO: change to load a small image and tile it similar to the blockade class
         * don't forget to change ThreadedRenderer
         */
        if (background == null) {
            background = Images.createVolatileImage(i.getWidth(null), i.getHeight(null), Transparency.OPAQUE);
        }
        Graphics2D g = (Graphics2D) background.getGraphics();
        g.clearRect(0, 0, Main.game.getWidth(), Main.game.getWidth());
        //g.drawImage(i.getScaledInstance(Main.game.getWidth(), Main.game.getHeight(), Image.SCALE_SMOOTH), 0, 0, null);
        g.drawImage(i,0,0,null);
        g.dispose();
    }
}
